<template>
  <div class="cart-container">
    <h2>购物车</h2>
    
    <div v-if="cartItems.length === 0" class="empty-cart">
      <i class="fas fa-shopping-cart"></i>
      <p>购物车是空的</p>
      <router-link to="/" class="continue-shopping">继续购物</router-link>
    </div>

    <div v-else class="cart-content">
      <div class="cart-items">
        <div v-for="item in cartItems" :key="item.id" class="cart-item">
          <div class="item-image">
            <img :src="item.image" :alt="item.name">
          </div>
          
        <div class="item-info">
            <div class="item-header">
          <h3>{{ item.name }}</h3>
              <span class="item-type" :class="item.productType">{{ getProductTypeLabel(item.productType) }}</span>
            </div>
            
            <p class="item-description">{{ item.description }}</p>
            
            <div class="item-price">
              <span class="price">¥{{ item.price }}</span>
              <div class="quantity-control">
                <button @click="decreaseQuantity(item)" :disabled="item.quantity <= 1">-</button>
                <input type="number" v-model.number="item.quantity" min="1" max="99">
                <button @click="increaseQuantity(item)" :disabled="item.quantity >= 99">+</button>
              </div>
            </div>
          </div>

          <div class="item-actions">
            <button @click="removeFromCart(item)" class="remove-btn">
            </button>
        </div>
        </div>
      </div>

      <div class="cart-summary">
        <h3>订单摘要</h3>
        
        <div class="summary-item">
          <span>商品总数</span>
          <span>{{ totalItems }} 件</span>
        </div>
        
        <div class="summary-item">
          <span>商品总价</span>
          <span class="total-price">¥{{ totalPrice }}</span>
        </div>

        <div class="summary-item">
          <span>运费</span>
          <span>¥{{ shipping }}</span>
        </div>

        <div class="summary-item grand-total">
          <span>应付总额</span>
          <span class="grand-total-price">¥{{ grandTotal }}</span>
        </div>

        <button @click="checkout" class="checkout-btn">
          结算
        </button>
      </div>
    </div>
  </div>
</template>

<script>
import { computed } from 'vue'
import { useStore } from 'vuex'
import { useRouter } from 'vue-router'

export default {
  name: 'Cart',
  setup() {
    const store = useStore()
    const router = useRouter()

    const cartItems = computed(() => store.state.cart)
    const totalItems = computed(() => cartItems.value.reduce((sum, item) => sum + item.quantity, 0))
    const totalPrice = computed(() => cartItems.value.reduce((sum, item) => sum + item.price * item.quantity, 0))
    const shipping = computed(() => totalPrice.value > 1000 ? 0 : 50)
    const grandTotal = computed(() => totalPrice.value + shipping.value)

    const getProductTypeLabel = (type) => {
      const types = {
        motorcycle: '摩托车',
        gear: '骑行装备',
        parts: '配件改装'
      }
      return types[type] || type
    }

    const decreaseQuantity = (item) => {
      if (item.quantity > 1) {
        store.commit('updateQuantity', {
          productId: item.id,
          quantity: item.quantity - 1
        })
      }
    }
    
    const increaseQuantity = (item) => {
      if (item.quantity < 99) {
        store.commit('updateQuantity', {
          productId: item.id,
          quantity: item.quantity + 1
        })
    }
    }

    const removeFromCart = (item) => {
      store.commit('removeFromCart', item.id)
    }
    
    const checkout = () => {
      router.push('/checkout')
    }
    
    return {
      cartItems,
      totalItems,
      totalPrice,
      shipping,
      grandTotal,
      getProductTypeLabel,
      decreaseQuantity,
      increaseQuantity,
      removeFromCart,
      checkout
    }
  }
}
</script>

<style scoped>
.cart-container {
  padding: 40px;
  max-width: 1200px;
  margin: 0 auto;
  min-height: calc(100vh - 60px);
  background: var(--cf-white);
}

.cart-container h2 {
  color: var(--cf-black);
  border-bottom: 2px solid var(--cf-orange);
  padding-bottom: 10px;
  margin-bottom: 30px;
  font-size: 1.8em;
  text-transform: uppercase;
  letter-spacing: 1px;
}

.empty-cart {
  text-align: center;
  padding: 60px 20px;
  background: var(--cf-light-gray);
  border-radius: 12px;
  animation: fadeIn 0.5s ease;
}

.empty-cart i {
  font-size: 4em;
  color: var(--cf-gray);
  margin-bottom: 20px;
}

.empty-cart p {
  font-size: 1.2em;
  color: var(--cf-gray);
  margin-bottom: 20px;
}

.continue-shopping {
  display: inline-block;
  padding: 12px 30px;
  background: var(--cf-blue);
  color: var(--cf-white);
  text-decoration: none;
  border-radius: 8px;
  transition: all 0.3s ease;
}

.continue-shopping:hover {
  background: var(--cf-dark-blue);
  transform: translateY(-2px);
}

.cart-content {
  display: grid;
  grid-template-columns: 2fr 1fr;
  gap: 30px;
  animation: fadeIn 0.5s ease;
}

.cart-items {
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.cart-item {
  display: grid;
  grid-template-columns: 120px 1fr auto;
  gap: 20px;
  padding: 20px;
  background: var(--cf-light-gray);
  border-radius: 12px;
  transition: all 0.3s ease;
}

.cart-item:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 15px rgba(0,0,0,0.1);
}

.item-image {
  width: 120px;
  height: 120px;
  overflow: hidden;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: white;
}

.item-image img {
  width: 100%;
  height: 100%;
  object-fit: contain;
  transition: transform 0.3s ease;
}

.item-image:hover img {
  transform: scale(1.05);
}

.item-info {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.item-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 15px;
}

.item-header h3 {
  margin: 0;
  font-size: 1.2em;
  color: var(--cf-black);
}

.item-type {
  display: inline-block;
  padding: 4px 10px;
  border-radius: 15px;
  font-size: 0.8em;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: white;
  align-self: flex-start;
}

.item-type.motorcycle {
  background: linear-gradient(135deg, #0078A0 0%, #005c80 100%);
}

.item-type.gear {
  background: linear-gradient(135deg, #0090B8 0%, #0078A0 100%);
}

.item-type.parts {
  background: linear-gradient(135deg, #00A0C8 0%, #0090B8 100%);
}

.item-type.maintenance {
  background: #6B7280;
  color: white;
}

.item-description {
  color: var(--cf-gray);
  font-size: 0.9em;
  margin: 0;
}

.item-price {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: auto;
}

.price {
  font-size: 1.4em;
  color: var(--cf-orange);
  font-weight: bold;
}

.quantity-control {
  display: flex;
  align-items: center;
  border: 1px solid var(--cf-gray);
  border-radius: 8px;
  overflow: hidden;
}

.quantity-control button {
  width: 36px;
  height: 36px;
  border: none;
  background: var(--cf-light-gray);
  cursor: pointer;
  transition: all 0.3s ease;
}

.quantity-control button:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.quantity-control button:hover:not(:disabled) {
  background: var(--cf-gray);
}

.quantity-control input {
  width: 50px;
  height: 36px;
  border: none;
  text-align: center;
  font-size: 1em;
}

.item-actions {
  display: flex;
  align-items: flex-start;
}

.remove-btn {
  width: 36px;
  height: 36px;
  border: none;
  background: #fee2e2;
  color: #ef4444;
  cursor: pointer;
  transition: all 0.3s ease;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
}

.remove-btn::before,
.remove-btn::after {
  content: '';
  position: absolute;
  width: 16px;
  height: 2px;
  background-color: currentColor;
  transform-origin: center;
}

.remove-btn::before {
  transform: rotate(45deg);
}

.remove-btn::after {
  transform: rotate(-45deg);
}

.remove-btn:hover {
  background: #fecaca;
  color: #dc2626;
  transform: scale(1.1);
}

.remove-btn i {
  display: none;
}

.cart-summary {
  background: var(--cf-light-gray);
  padding: 30px;
  border-radius: 12px;
  position: sticky;
  top: 20px;
}

.cart-summary h3 {
  color: var(--cf-black);
  margin: 0 0 20px 0;
  font-size: 1.4em;
  border-bottom: 2px solid var(--cf-orange);
  padding-bottom: 10px;
}

.summary-item {
  display: flex;
  justify-content: space-between;
  margin-bottom: 15px;
  color: var(--cf-gray);
}

.summary-item.grand-total {
  margin-top: 20px;
  padding-top: 20px;
  border-top: 1px solid var(--cf-gray);
  color: var(--cf-black);
  font-weight: bold;
  font-size: 1.2em;
}

.total-price, .grand-total-price {
  color: var(--cf-orange);
  font-weight: bold;
}

.checkout-btn {
  width: 100%;
  padding: 15px;
  background: var(--cf-blue);
  color: var(--cf-white);
  border: none;
  border-radius: 8px;
  font-size: 1.1em;
  cursor: pointer;
  transition: all 0.3s ease;
  margin-top: 20px;
}

.checkout-btn:hover {
  background: var(--cf-dark-blue);
  transform: translateY(-2px);
}

@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateY(20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@media (max-width: 768px) {
  .cart-container {
    padding: 20px;
  }

  .cart-content {
    grid-template-columns: 1fr;
  }

  .cart-item {
    grid-template-columns: 100px 1fr;
  }

  .item-image {
    width: 100px;
    height: 100px;
  }

  .item-actions {
    grid-column: 1 / -1;
    justify-content: flex-end;
  }

  .cart-summary {
    position: static;
  }
}
</style>